from flask import Flask, send_from_directory,jsonify
from api import api_bp
from config import Config
from flask_cors import CORS
def create_app():
    app = Flask(__name__)
    CORS(app)
    app.config.from_object(Config)

    # 注册蓝图
    app.register_blueprint(api_bp)

    # 添加根路由（应该在create_app函数内部）
    @app.route('/')
    def serve_index():
        return send_from_directory(app.static_folder, 'index.html')

    # 标记测试页面路由
    @app.route('/mark')
    def serve_mark_index():
        return send_from_directory(app.static_folder, 'mark_index.html')

    @app.route('/routes')
    def list_routes():
        routes = []
        for rule in app.url_map.iter_rules():
            routes.append({
                'endpoint': rule.endpoint,
                'methods': sorted(rule.methods),
                'path': str(rule)
            })
        return jsonify({'routes': routes})
    # 添加错误处理
    @app.errorhandler(404)
    def not_found(error):
        return {"error": "Not found"}, 404

    @app.errorhandler(500)
    def server_error(error):
        return {"error": "Server error"}, 500

    return app

if __name__ == '__main__':
    app = create_app()
    app.run(debug=True,port=9999)